package xuegao.study.mall.admin.controller;

import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.admin.feign.UmsAdminClient;
import xuegao.study.mall.admin.interceptor.IpLimit;
import xuegao.study.mall.common.api.CommonPage;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.domain.UserDto;
import xuegao.study.mall.common.model.ums.UmsAdmin;
import xuegao.study.mall.common.model.ums.UmsRole;
import xuegao.study.mall.common.param.ums.UmsAdminLoginParam;
import xuegao.study.mall.admin.service.ums.UmsAdminService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class UmsAdminController {

    Logger logger = LoggerFactory.getLogger(UmsAdminController.class);

    @Autowired
    private UmsAdminClient umsAdminClient;

    @Autowired
    private UmsAdminService umsAdminService;

    @ApiOperation("用户名密码认证")
    @PostMapping(value = "/login")
    public CommonResult login(@RequestBody UmsAdminLoginParam param) {
        return umsAdminService.login(param);
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @GetMapping("/info")
    public CommonResult getAdminInfo(HttpServletRequest request) {
        String userStr = request.getHeader("user");
        return umsAdminClient.getAdminInfo(userStr);
    }

    @ApiOperation("用户注册")
    @PostMapping(value = "/register")
    public CommonResult register(@RequestBody UmsAdmin umsAdmin) {
        return umsAdminService.register(umsAdmin);
    }

    @ApiOperation("登出功能")
    @PostMapping("/logout")
    public CommonResult logout() {
        return CommonResult.success(null);
    }

    @ApiOperation("删除用户")
    @PostMapping("/delete/{id}")
    public CommonResult<Integer> deleteResource(@PathVariable("id") Long id) {
        return umsAdminClient.deleteAdmin(id);
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/list")
    @IpLimit
    public CommonResult<CommonPage<UmsAdmin>> listRole(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "name", defaultValue = "") String name){
        return umsAdminClient.listAdmin(pageNum, pageSize, name);
    }

    @ApiOperation("更新用户状态")
    @PostMapping(value = "/updateStatus/{adminId}")
    public CommonResult updateAdminStatus(@PathVariable("adminId") Long adminId, @RequestParam("status") Integer status) {
        return umsAdminClient.updateAdminStatus(adminId, status);
    }

    @GetMapping("/role/{adminId}")
    public CommonResult<List<UmsRole>> adminRoles(@PathVariable("adminId") Long adminId) {
        return umsAdminClient.adminRoles(adminId);
    }

    @ApiOperation("更新用户角色")
    @PostMapping("/role/update")
    public CommonResult<Integer> allocRoles(@RequestParam Long adminId, @RequestParam List<Long> roleIds) {
        return umsAdminClient.allocRoles(adminId, roleIds);
    }

    @PostMapping("/update/{id}")
    public CommonResult<Integer> update(@PathVariable("id") Long id, @RequestBody UmsAdmin umsAdmin) {
        return umsAdminClient.update(id, umsAdmin);
    }

    @ApiOperation("根据用户名获取通用用户信息")
    @GetMapping(value = "/loadByUsername")
    public UserDto loadUserByUsername(@RequestParam String username) {
        return umsAdminClient.loadUserByUsername(username);
    }
}
